<template>
  <Dialog :appDialog="appDialog">
    <div class="top-box">
      <el-radio-group v-model="tabIndex" @change="chooseTab">
        <el-radio-button label="0">自动化规则</el-radio-button>
        <el-radio-button label="1">提醒设置</el-radio-button>
      </el-radio-group>
    </div>
    <section v-if="tabIndex == 0">
      <div style="margin-bottom: 20px"
        >自动化规则:
        <el-switch :value="setInfo.auto_rule" :active-value="1" :inactive-value="2" @click="changeStatus('auto_rule')" />
        <span style="margin: 0 10px"></span
      ></div>
      <Rules ref="RulesRef" v-if="setInfo.auto_rule == 1" :tableList="appDialog.tableList" />
    </section>
    <section v-if="tabIndex == 1">
      <Remind ref="RemindRef" :tableList="appDialog.tableList" :setInfo="setInfo" @changeStatus="changeStatus" />
    </section>
  </Dialog>
</template>


<script setup lang="ts">
import Rules from './Rules.vue'
import Remind from './Remind.vue'
import Dialog from '@/components/Dialog.vue'
import { stageAdvancedInfo, stageAdvanced, triggerTask } from '../../api'
import { ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
const tabIndex = ref('0')
const emit = defineEmits(['refresh'])
const RulesRef = ref(null)
const RemindRef = ref(null)
// 设置开关数据
const setInfo = ref({
  auto_rule: 1,
  remind: 1,
  cust_stage_advance_id: ''
})

const chooseTab = (val: number) => {
  tabIndex.value = val
}

const submitHandel = () => {
  if(tabIndex.value == 0 && setInfo.value.auto_rule!=1){
    appDialog.value.visible = false
    return;
  }
  const params = {
    type: tabIndex.value, //0自动化规则,1提醒设置
    stage_id: tabIndex.value == 0 ?  RulesRef.value.tabIndexid  : RemindRef.value.tabIndexid
  }

  console.log(params, 'setInfosetInfo')
  triggerTask(params).then((r: any) => {
    ElMessage.success('触发规则任务成功')
    emit('refresh')
    appDialog.value.visible = false
  })
}
const submitcandel = () => {
  // emit('refresh')
  appDialog.value.visible = false
}

const appDialog = ref({
  visible: false,
  title: '高级设置',
  width: '994px',
  cancel: submitcandel,
  submit: submitHandel,
  cancelText: '取消',
  submitText: '确认',
  tableList: [
    { title: '新客户', sort: 1 },
    { title: '潜在客户', sort: 2 },
    { title: '意向购买客户', sort: 3 },
    { title: '成单客户', sort: 4 },
    { title: '无意向客户', sort: 7 }
  ] //当前阶段
})
// 获取状态
const stageAdvancedInfoFun = () => {
  stageAdvancedInfo({}).then((r: any) => {
    setInfo.value = {
      auto_rule: (r && r.auto_rule) || 2,
      remind: (r && r.remind) || 2,
      cust_stage_advance_id: (r && r.id) || ''
    }
  })
}

// 更改状态
const changeStatus = (type: any) => {
  let { auto_rule, remind, cust_stage_advance_id } = setInfo.value
  if (type == 'auto_rule') {
    auto_rule = auto_rule == 1 ? 2 : 1
  } else {
    remind = remind == 1 ? 2 : 1
  }
  if (!setInfo.value.cust_stage_advance_id) {
    stageAdvancedInfoFun()
    ElMessage.error('当前数据异常请稍后重试')
    return
  }
  stageAdvanced({ auto_rule, remind, cust_stage_advance_id }).then((r: any) => {
    stageAdvancedInfoFun()
  })
}

watch(
  () => appDialog.value.visible,
  (val: boolean) => {
    if (val) {
      tabIndex.value = 0
      stageAdvancedInfoFun()
    }
  }
)

defineExpose({ appDialog })
</script>

<style lang="scss" scoped>
.top-box {
  margin-bottom: 24px;
}
</style>
